//
//  NetEngineServer.m
//  carcareIOS
//
//  Created by ileo on 16/6/13.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "NetEngineServer.h"
#import "NetDealMgr.h"

@implementation NetEngineServer

+(void)config{
    [NetEngine setupDefaultConfig:[NetEngineServer iczConfig]];
    [NetEngine setupDefaultTipsConfig:[NetEngineServer tipsConfig]];
}

+(id<NetRequestConfig>)iczConfig{
    return [[NetICZConfig alloc] init];
}

+(id<NetRequestConfig>)iczImageConfig{
    return [[NetICZImageConfig alloc] init];
}

+(id<NetRequestConfig>)iczMallConfig{
    return [[NetICZMallConfig alloc] init];
}

+(id<NetTipsConfig>)tipsConfig{
    return [[NetTipsConfig alloc] init];
}

#ifdef DEBUG
+(id<NetRequestConfig>)testConfig{
    return [[NetTestConfig alloc] init];
}
#endif

@end


@implementation NetTipsConfig

-(void)showLoading{
    [AlertMgr showWaiting];
}

-(void)disappearLoading{
    [AlertMgr dismiss:TYPE_VIEW_WAIT];
}

-(void)showTips:(NSString *)tips type:(RESPONSE_TIPS_TYPE)type{
    switch (type) {
        case RESPONSE_TIPS_FAIL:
            [AlertMgr showShortWarnTextTips:@[tips]];
            break;
        case RESPONSE_TIPS_LINK_FAIL:
            [AlertMgr showShortNoNetworkTextTips:@[tips]];
            break;
        default:
            break;
    }
}

@end

@implementation NetICZConfig

-(NSString *)requestMainURL{
    return @"http://app1.ichezheng.com/CarcareService";
}

-(BOOL)requestIsSuccessWithResponse:(id)responseObject{
    return [responseObject[@"errFlag"] boolValue];
}

-(NSString *)requestMessageWithResponse:(id)responseObject{
    return responseObject[@"errMsg"];
}

-(void)requestHandleWithErrorCodeWithResponse:(id)responseObject{//NET_ERROR_SESSION
    [NetDealMgr dealWithNetError:[responseObject[@"errCode"] integerValue] doThis:nil];
}

-(NSDictionary *)requestLinkErrorMessageWithError:(NSError *)error response:(NSURLResponse *)response{
    return @{@"errMsg":@"网络出现异常"};
}

@end

#ifdef DEBUG
@implementation NetTestConfig
-(NSString *)requestMainURL{
//    return @"http://10.8.6.2:8080/CarcareService";//违章
    return @"http://115.159.49.124:20001/CarcareService";
    return @"http://app1.ichezheng.com:10010/CarcareService";
}

-(BOOL)requestIsSuccessWithResponse:(id)responseObject{
    return [responseObject[@"errFlag"] boolValue];
}

-(NSString *)requestMessageWithResponse:(id)responseObject{
    return responseObject[@"errMsg"];
}

-(void)requestHandleWithErrorCodeWithResponse:(id)responseObject{//NET_ERROR_SESSION
    [NetDealMgr dealWithNetError:[responseObject[@"errCode"] integerValue] doThis:nil];
}

-(NSDictionary *)requestLinkErrorMessageWithError:(NSError *)error response:(NSURLResponse *)response{
    return @{@"errMsg":@"网络出现异常"};
}

@end
#endif


@implementation NetICZImageConfig

-(NSString *)requestMainURL{
    return @"http://image.ichezheng.com/Image";
}

-(BOOL)requestIsSuccessWithResponse:(id)responseObject{
    return [responseObject[@"code"] integerValue] == 0;
}

-(NSString *)requestMessageWithResponse:(id)responseObject{
    return responseObject[@"message"];
}

-(void)requestHandleWithErrorCodeWithResponse:(id)responseObject{//NET_ERROR_SIGN
    [NetDealMgr dealWithNetError:[responseObject[@"code"] integerValue] doThis:nil];
}

-(NSDictionary *)requestLinkErrorMessageWithError:(NSError *)error response:(NSURLResponse *)response{
    return @{@"message":@"网络出现异常"};
}

@end


@implementation NetICZMallConfig

-(NSString *)requestMainURL{
    return @"http://mall.ichezheng.com/CarcareWeChatService";
}

-(BOOL)requestIsSuccessWithResponse:(id)responseObject{
    return [responseObject[@"errFlag"] boolValue];
}

-(NSString *)requestMessageWithResponse:(id)responseObject{
    return responseObject[@"errMsg"];
}

-(void)requestHandleWithErrorCodeWithResponse:(id)responseObject{//NET_ERROR_SESSION
    [NetDealMgr dealWithNetError:[responseObject[@"errCode"] integerValue] doThis:nil];
}

-(NSDictionary *)requestLinkErrorMessageWithError:(NSError *)error response:(NSURLResponse *)response{
    return @{@"errMsg":@"网络出现异常"};
}

@end

